﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCMusicStore.Models;
using MVCMusicStore.ViewModels;
namespace MVCMusicStore.Controllers
{
    [Authorize(Roles="admin")]
    public class StoreManagerController : Controller
    {
        MusicStoreEntities db = new Models.MusicStoreEntities();
        //
        // GET: /StoreManager/

        public ActionResult Index()
        {
            var albums = db.Albums.Include("Genre").Include("Artist").ToList();
            return View(albums);
        }

        //
        // GET: /StoreManager/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            var viewModel = new StoreManagerViewModel
            {
                Album = new Album(),
                Genres = db.Genres.ToList(),
                Artists = db.Artists.ToList()
            };
            return View(viewModel);
        } 

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Album album)
        {
            try
            {
                // TODO: Add insert logic here
                db.AddToAlbums(album);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                var viewModel = new StoreManagerViewModel
                {
                    Album = new Album(),
                    Genres = db.Genres.ToList(),
                    Artists = db.Artists.ToList()
                };
                return View(viewModel);
            }
        }
        
        //
        // GET: /StoreManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            var viewModel = new StoreManagerViewModel
            {
                Album = db.Albums.Single(a => a.AlbumId == id),
                Genres = db.Genres.ToList(),
                Artists = db.Artists.ToList()
            };
            return View(viewModel);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var album = db.Albums.Single(a => a.AlbumId == id);
            try
            {
                // TODO: Add update logic here
                
                /*
                 * The second parameter Album above is the prefix of collection which looks like this collection["Album.Title"]
                 * So, the UpdateModel(album, "Album"); the object album will be updated with values gotten from collection items which have a prefix "Album"
                 */
                UpdateModel(album, "Album");
                //make change to database
                db.SaveChanges();
                return RedirectToAction("Index");

            }
            catch
            {
                var viewModel = new StoreManagerViewModel
                {
                    Album = album,
                    Genres = db.Genres.ToList(),
                    Artists = db.Artists.ToList()
                };
                return View(viewModel);

            }
        }

        //
        // GET: /StoreManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            var model = db.Albums.Single(a => a.AlbumId == id);
            return View(model);
        }

        //
        // POST: /StoreManager/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            string albumTitle = string.Empty;
            var album = db.Albums.Include("OrderDetails").Include("Carts").Single(a => a.AlbumId == id);
            try
            {
                albumTitle = album.Title;
                // TODO: Add delete logic here
                db.Albums.DeleteObject(album);
                db.SaveChanges();
                return RedirectToAction("Deleted", new { albumTitle = albumTitle });
            }
            catch
            {
                return View(album);
            }
        }
        
        //
        // GET: /StoreManager/Delete/5

        public ActionResult Deleted()
        {
            return View();
        }
    }
}
